#!/bin/sh

. /etc/rc.d/PUPSTATE
case $PUPMODE in
	5) ERRORMSG="$(gettext 'Please create puppy save first')" ;;
	2) ERRORMSG="$(gettext 'Underdog is not applicable on fully installed puppy. It only works on frugal installation.')" ;;
esac
if [ "$ERRORMSG" ] ; then
	echo "$ERRORMSG"
	[ $DISPLAY ] && /usr/lib/gtkdialog/box_ok "Underdog" error "$ERRORMSG"
	exit 1
fi

underdog_lnx=/initrd${PUP_HOME}${PSUBDIR}/underdog.lnx

function underdog_func() {
	if [ "$1" = "start" ]; then
		if [ "$TARGET_DRV" != "" ]; then
			echo "$TARGET_DRV" > ${underdog_lnx}
			echo "Underdog on $TARGET_DRV enabled"
		elif [ "$2" != "" ]; then
			echo "$2" > ${underdog_lnx}
			echo "Underdog on $2 enabled"
		else
			echo "device name not specified"
		fi
	elif [ "$1" = "stop" ]; then
		if [ -f ${underdog_lnx} ]; then
			rm -f ${underdog_lnx}
			echo "Underdog is disabled"
		else
			echo "Underdog file not found"
		fi
	else
		echo "Usage: underdog [start|stop] <drive_name (e.g sda1)>"
	fi
}

if [ $1 ] || [ ! $DISPLAY ] ; then
	underdog_func $@
	exit
fi

####################################################
#                      GUI
####################################################

drvitem=""
status=""
drvhost=""

if [ -f ${underdog_lnx} ]; then
	drvhost=$(cat ${underdog_lnx})
	status="$(gettext 'ENABLED')"
	start_button="false"
	stop_button="true"
	combobox="false"
else
	drvhost="$(gettext 'NONE')"
	status="$(gettext 'DISABLED')"
	start_button="true"
	stop_button="false"
	combobox="true"
fi

XHOME=$(mount | cut -f 1,3 -d ' ' | grep "/mnt/dev_save" | cut -f 1 -d ' ')
for drv in `probepart | cut -f 1 -d '|' | grep -E "/(s|h)d[a-z][0-9]" | grep -v "${XHOME}" | cut -f 3 -d "/"`
do
	drvitem="$drvitem<item>$drv</item>"
done

help_box() {
	UD_1="$(gettext "The Underdog Distro")"
	UD_2="$(gettext "At bootup Puppy will read underdog.lnx and will mount the partition as the bottom layer. If that partition happens to have a Linux distro installed in it, then the entire distro filesystem will 'show through' on the top layer of Puppy's unionfs.")"
	UD_3="$(gettext "It will look like a normal Puppy, same desktop, but everything in the underlying distro is available to be executed. All the applications, compile environment, package manager, etc.")"
	echo -e "${UD_1}\n\n${UD_2}\n\n${UD_3}" > /tmp/box_help
	/usr/lib/gtkdialog/box_help
}
export -f help_box

WINDOW_MSG="$(/usr/lib/gtkdialog/xml_info 0 puppy.svg 32 "$(gettext "This tool allows puppy to use the partition of another linux distro.")")"

export UNDERDOG_DIALOG='
<window title="Underdog Linux" image-name="/usr/share/pixmaps/puppy/preferences.svg">
<vbox>
  '${WINDOW_MSG}'
  <text use-markup="true"><label>"'$(gettext '<b>WARNING</b>: For advanced users only')'"</label></text>
  <frame>
    <vbox>
      <text use-markup="true"><label>"'$(gettext 'Status:')' '${status}'"</label></text>
      <text use-markup="true"><label>"'$(gettext 'Partition in use:')' '${drvhost}'"</label></text>
    </vbox>
  </frame>
  <hbox>
   <text><label>'$(gettext 'Partition Name:')'</label></text>
   <combobox>
     <variable>TARGET_DRV</variable>
     <sensitive>"'${combobox}'"</sensitive>
      '${drvitem}'
     </combobox>
   </hbox>
  <hbox>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon help`"'
      <label>"'$(gettext 'Help')'"</label>
      <action>help_box &</action>
    </button>
    <text space-expand="true" space-fill="false"><label>" "</label></text>
   <button>
     <label>'$(gettext 'Write underdog.lnx')'</label>
     <sensitive>"'${start_button}'"</sensitive>
     '$(/usr/lib/gtkdialog/xml_button-icon dialog-complete.svg)'
     <action>'${0}' start</action>
     <action>/usr/lib/gtkdialog/box_ok "Underdog" info "'$(gettext 'Underdog is enabled. Please restart Puppy to apply changes')'"</action>
     <action type="exit">exit</action>
   </button>
   <button>
     <label>'$(gettext 'Delete underdog.lnx')'</label>
     <sensitive>"'${stop_button}'"</sensitive>
     '$(/usr/lib/gtkdialog/xml_button-icon dialog-error.svg)'
     <action>'${0}' stop</action>
     <action>/usr/lib/gtkdialog/box_ok "Underdog" info "'$(gettext 'Underdog is disabled. Please restart Puppy to apply changes')'"</action>
     <action type="exit">exit</action>
   </button>
   <button>
      '$(/usr/lib/gtkdialog/xml_button-icon quit.svg)'
      <label>Quit</label>
      <action type="exit">Cancel</action>
   </button>
  </hbox>
</vbox>
</window>
'

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog --center --program=UNDERDOG_DIALOG

### END ###
